﻿using frTextbox;
using NDK.Module.Model;
using NDK.Module.StationLogic;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;


namespace NDK.Frm
{
    public partial class Dlg_loginMes : Form
    {
        public Dlg_loginMes ()
        {
            InitializeComponent();
            if(Common.SwMes != null)
            {
                var prop = typeof(SunWayMes).GetProperties().ToList();
                foreach(Control item in this.Controls)
                {
                    if(item is frTextBox)
                    {
                        var p = prop.SingleOrDefault(x => x.Name == item.Tag.ToString());
                        item.Text = p.GetValue(Common.SwMes).ToString();
                    }
                }
            }
            else
            {
                Common.SwMes = new SunWayMes();
            }
        }

        private void bt_login_Click (object sender, EventArgs e)
        {
            var prop = typeof(SunWayMes).GetProperties().ToList();
            foreach(Control item in this.Controls)
            {
                if(item is frTextBox)
                {
                    if (!string.IsNullOrEmpty(item.Text))
                    {
                        var p = prop.SingleOrDefault(x => x.Name == item.Tag.ToString());
                        p.SetValue(Common.SwMes, Convert.ChangeType(item.Text, p.PropertyType));
                    }
                }
            }
            string json = JsonConvert.SerializeObject(Common.SwMes, Formatting.Indented);
            File.WriteAllText($"{Common.pointCheckPath}\\Mes.json", json);
        }


    }
}
